package com.zx.map.service.impl;

import com.zx.map.dao.CustAcctLogDao;
import com.zx.map.dao.model.CustAcctLog;
import com.zx.map.dao.model.CustAcctLogExample;
import com.zx.map.service.BaseAcctInfoService;
import com.zx.map.service.CustAcctLogService;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.domain.CustAcctLogDownloadDomain;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.CustAcctLogModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.utils.MyBeanUtils;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.domain.CustAcctLogQueryDomain;
import com.zx.mobile.domain.CustAcctLogSummary;
import com.zx.platform.utils.PmpUtils;
import com.zx.platform.utils.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 流水记录
 * 所有查询   都涉及层级
 */
@Service
public class CustAcctLogServiceImpl implements CustAcctLogService {

	@Autowired
	private CustAcctLogDao custAcctLogDao;
	@Autowired
	private MerchantInfoService merchantInfoService;//商户表

	@Autowired
	private BaseAcctInfoService baseAcctInfoService;

	/**
	 * 根据查询 和当前用户  初始化查询条件
	 *
	 * @param custAcctLogQueryDomain
	 * @param curAgentInfoModel
	 * @return
	 */
	private CustAcctLogExample initExampleCretia(CustAcctLogQueryDomain custAcctLogQueryDomain, AgentInfoModel curAgentInfoModel) {
		//查询当前代理商 发展的用户
		MerchantInfoModel merchantInfoModel=new MerchantInfoModel();
		MyBeanUtils.copyProperties(custAcctLogQueryDomain,merchantInfoModel);
		merchantInfoModel.setBeginDate("");
		merchantInfoModel.setEndDate("");
		List<String> merchantIdList = merchantInfoService.queryListMerchantIdByAgentInfo(curAgentInfoModel,merchantInfoModel);

		CustAcctLogExample example = new CustAcctLogExample();
		CustAcctLogExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(custAcctLogQueryDomain.getSysAccount())) {
			criteria.andCustSysaccountLike("%"+custAcctLogQueryDomain.getSysAccount()+"%");
		}


		if (StringUtils.isNotBlank(custAcctLogQueryDomain.getBeginDate())) {
			criteria.andCreateDateBetween(PmpUtils.strToDate(
					custAcctLogQueryDomain.getBeginDate(), "yyyy-MM-dd"), PmpUtils
					.addDate(PmpUtils.strToDate(custAcctLogQueryDomain.getEndDate(),
							"yyyy-MM-dd"), 1));
		}
		merchantIdList.add("-1");
		criteria.andCustIdIn(merchantIdList);

		return example;
	}

	/**
	 * 分页查询 当前用户对应的下级代理商交易流水
	 *
	 * @return
	 */
	public Map getPageByDomain(CustAcctLogQueryDomain custAcctLogQueryDomain, AgentInfoModel curAgentInfoModel) {
		CustAcctLogExample example = initExampleCretia(custAcctLogQueryDomain, curAgentInfoModel);
		Map<String, Object> map = new HashMap<String, Object>();
		int pageBegin = (custAcctLogQueryDomain.getPageNum() - 1) * custAcctLogQueryDomain.getPageSize();
		List<CustAcctLog> list = custAcctLogDao.getPageByExample(custAcctLogQueryDomain.getPageSize(), pageBegin, example);
		int count = custAcctLogDao.countListByExample(example);
		map.put("count", count);
		map.put("list", list);
		return map;
	}


	/**
	 * 获取总记录数
	 *
	 * @return
	 * @throws DataAccessException
	 */
	public int getPageSizeByDomain(CustAcctLogQueryDomain custAcctLogQueryDomain, AgentInfoModel curAgentInfoModel)
			throws DataAccessException {
		CustAcctLogExample example = initExampleCretia(custAcctLogQueryDomain, curAgentInfoModel);
		return custAcctLogDao.countListByExample(example);
	}

	/**
	 * 查询所有的记录汇总
	 *
	 * @param custAcctLogQueryDomain
	 * @param curAgentInfoModel
	 * @return
	 */
	@Override
	public List<CustAcctLogSummary> queryAcctLogSummaryList(CustAcctLogQueryDomain custAcctLogQueryDomain, AgentInfoModel curAgentInfoModel) {
		List<CustAcctLog> custAcctLogList = getListByDomain(custAcctLogQueryDomain, curAgentInfoModel);
		// 提供商户map
		List<CustAcctLogSummary> custAcctLogSummaryList = conveterToSummaryReportModel(custAcctLogList);
		return custAcctLogSummaryList;
	}

	/**
	 * @param custAccLogQueryDomain
	 * @param curAgentInfoModel
	 * @return
	 */
	@Override
	public List<CustAcctLogDownloadDomain> queryAcctLogReportList(CustAcctLogQueryDomain custAccLogQueryDomain, AgentInfoModel curAgentInfoModel) {
		List<CustAcctLog> custAcctLogList = getListByDomain(custAccLogQueryDomain, curAgentInfoModel);
		List<CustAcctLogDownloadDomain> custAcctLogDownloadDomainList = conveterToReportModel(custAcctLogList);
		return custAcctLogDownloadDomainList;
	}

	/**
	 * 分页获取交易信息报表
	 *
	 * @param custAccLogQueryDomain
	 * @param curAgentInfoModel
	 * @return
	 */
	@Override
	public Map queryAcctLogReportPageByDomain(CustAcctLogQueryDomain custAccLogQueryDomain, AgentInfoModel curAgentInfoModel) {
		Map<String, Object> map = getPageByDomain(custAccLogQueryDomain, curAgentInfoModel);
		List<CustAcctLog> custAcctLogList = (List) map.get("list");
		List<CustAcctLogDownloadDomain> reportList = conveterToReportModel(custAcctLogList);
		map.put("list", reportList);
		return map;
	}


	/**
	 * 订单查询条件获取交易信息
	 *
	 * @param custAcctLogQueryDomain
	 * @param curAgentInfoModel
	 * @return
	 * @throws org.springframework.dao.DataAccessException
	 */
	@Override
	public List<CustAcctLog> getListByDomain(CustAcctLogQueryDomain custAcctLogQueryDomain, AgentInfoModel curAgentInfoModel) {
		CustAcctLogExample example = initExampleCretia(custAcctLogQueryDomain, curAgentInfoModel);
		List<CustAcctLog> list = custAcctLogDao.getListByExample(example);
		return list;
	}

	/**
	 * 根据Model查找列表
	 */
	public List<CustAcctLog> getListByModel(CustAcctLogModel model)
			throws DataAccessException {
		CustAcctLogExample example = new CustAcctLogExample();
		CustAcctLogExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getCustSysaccount())) {
			criteria.andCustSysaccountEqualTo(model.getCustSysaccount());
		}
		List<CustAcctLog> list = custAcctLogDao.getListByExample(example);
		return list;
	}

	/**
	 * 新增
	 */
	public Integer add(CustAcctLogModel model) throws DataAccessException {
		CustAcctLog temp = new CustAcctLog();
		MyBeanUtils.copyProperties(model, temp);
		Integer key = custAcctLogDao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public Integer update(CustAcctLogModel model) throws DataAccessException {
		CustAcctLog temp = new CustAcctLog();
		MyBeanUtils.copyProperties(model, temp);
		return custAcctLogDao.update(temp);
	}

	/**
	 * 删除
	 */
	public void delete(CustAcctLogModel model) throws DataAccessException {
		CustAcctLog temp = new CustAcctLog();
		MyBeanUtils.copyProperties(model, temp);
		custAcctLogDao.delete(temp);
	}


	/**
	 * 转换日志记录对象到view曾显示对象
	 *
	 * @param custAcctLogList
	 * @return
	 */
	private List<CustAcctLogDownloadDomain> conveterToReportModel(List<CustAcctLog> custAcctLogList) {
		List<CustAcctLogDownloadDomain> reportList = new ArrayList<CustAcctLogDownloadDomain>();
		for (CustAcctLog o : custAcctLogList) {
			CustAcctLogDownloadDomain temp = new CustAcctLogDownloadDomain();
			BeanUtils.copyProperties(o, temp);
			temp.setSysAccount(o.getCustSysaccount());
			temp.setRemark(StrUtils.getCustAcctLogRemarkById(o.getRemark()));
			MerchantInfoModel tempMer = merchantInfoService.getModelByKey(o
					.getCustId());
			temp.setMerchantName(tempMer.getMerchantName());
			temp.setTransType(StrUtils.getTransTypeById(o.getTransType()));
			reportList.add(temp);
		}
		return reportList;
	}

	/**
	 * 转换交易记录汇总对象到 model
	 *
	 * @param custAcctLogList
	 * @return
	 */
	private List<CustAcctLogSummary> conveterToSummaryReportModel(List<CustAcctLog> custAcctLogList) {
		Map<String, CustAcctLogSummary> custMap = new HashMap<String, CustAcctLogSummary>();
		for (CustAcctLog o : custAcctLogList) {
			String transType = o.getTransType();
			CustAcctLogSummary custAcctLogSummary = custMap
					.get(o.getCustSysaccount());
			if (custAcctLogSummary != null) {
				BigDecimal tranAmt = new BigDecimal(o.getTransAmt()
						.toString());
				if (SUConstants.TRANSTYPE_INCOME.equals(transType)) {
					custAcctLogSummary.setIncomeAmt(custAcctLogSummary.getIncomeAmt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_NOCARDINCOME.equals(transType)) {
					custAcctLogSummary.setNocardAmt(custAcctLogSummary.getNocardAmt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_CARDINCOME.equals(transType)) {
					custAcctLogSummary.setCardAmt(custAcctLogSummary.getCardAmt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_T0ENCASH.equals(transType)) {
					custAcctLogSummary.setT0Amt(custAcctLogSummary.getT0Amt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_T1ENCASH.equals(transType)) {
					custAcctLogSummary.setT1Amt(custAcctLogSummary.getT1Amt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_CREDITENCASH.equals(transType)) {
					custAcctLogSummary.setCreditAmt(custAcctLogSummary.getCreditAmt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_CONSUME.equals(transType)) {
					custAcctLogSummary.setConsumeAmt(custAcctLogSummary.getConsumeAmt().add(tranAmt));
				} else if (SUConstants.TRANSTYPE_SHAREPROFIT.equals(transType)) {
					custAcctLogSummary.setSpAmt(custAcctLogSummary.getSpAmt().add(tranAmt));
				}
			} else {
				custAcctLogSummary = new CustAcctLogSummary();
				MerchantInfoModel merchantInfoModel = merchantInfoService.getMerchantInfoModelBySysAccount(o.getCustSysaccount());
				//设置 实际户余额
				BaseAcctInfoModel baseAcct = baseAcctInfoService.getModelByKey(o.getCustId());
				custAcctLogSummary.setSysAccount(merchantInfoModel.getSysAccount());
				custAcctLogSummary.setMerchantName(merchantInfoModel.getMerchantName());
				custAcctLogSummary.setEndAcctNature(new BigDecimal(baseAcct.getAvlBal()));

				BigDecimal beginAcct = new BigDecimal(o.getTransAmt());
				if (SUConstants.TRANSTYPE_INCOME.equals(transType)) {
					custAcctLogSummary.setIncomeAmt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).subtract(beginAcct);
				} else if (SUConstants.TRANSTYPE_NOCARDINCOME.equals(transType)) {
					custAcctLogSummary.setNocardAmt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).subtract(beginAcct);
				} else if (SUConstants.TRANSTYPE_CARDINCOME.equals(transType)) {
					custAcctLogSummary.setCardAmt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).subtract(beginAcct);
				} else if (SUConstants.TRANSTYPE_T0ENCASH.equals(transType)) {
					custAcctLogSummary.setT0Amt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).add(beginAcct);
				} else if (SUConstants.TRANSTYPE_T1ENCASH.equals(transType)) {
					custAcctLogSummary.setT1Amt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).add(beginAcct);
				} else if (SUConstants.TRANSTYPE_CREDITENCASH.equals(transType)) {
					custAcctLogSummary.setCreditAmt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).add(beginAcct);
				} else if (SUConstants.TRANSTYPE_CONSUME.equals(transType)) {
					custAcctLogSummary.setConsumeAmt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).add(beginAcct);
				} else if (SUConstants.TRANSTYPE_SHAREPROFIT.equals(transType)) {
					custAcctLogSummary.setSpAmt(beginAcct);
					beginAcct = new BigDecimal(o.getAcctBal()).subtract(beginAcct);
				}
				// 记录期初余额
				custAcctLogSummary.setBeginAcct(beginAcct);
				custMap.put(o.getCustSysaccount(), custAcctLogSummary);
			}
			BigDecimal endAcct = custAcctLogSummary.getBeginAcct();
			endAcct = endAcct.add(custAcctLogSummary.getIncomeAmt())
					.add(custAcctLogSummary.getNocardAmt())
					.add(custAcctLogSummary.getCardAmt())
					.add(custAcctLogSummary.getSpAmt())
					.subtract(custAcctLogSummary.getT0Amt())
					.subtract(custAcctLogSummary.getT1Amt())
					.subtract(custAcctLogSummary.getCreditAmt())
					.subtract(custAcctLogSummary.getConsumeAmt());
			custAcctLogSummary.setEndAcct(endAcct);
		}
		Iterator iter = custMap.entrySet().iterator();
		List<CustAcctLogSummary> custAcctLogSummaryList = new ArrayList<CustAcctLogSummary>();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			CustAcctLogSummary val = (CustAcctLogSummary) entry.getValue();
			custAcctLogSummaryList.add(val);
		}
		return custAcctLogSummaryList;
	}
}
